<template>
  <!-- 登录界面所有内容 -->
  <div class="login_container">
    <!-- 登录盒子 -->
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avater_box">
        <img src="../assets/R-C.jpg" alt="" />
      </div>
      <el-form class="login_from" ref="loginFormRef" :model="Loginfrom" :rules="LoginfromRules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="Loginfrom.username" prefix-icon="el-icon-user">
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input type="password" v-model="Loginfrom.password" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button  type="primary" @click="login">登录</el-button>
          <el-button  type="info" @click="resetLoginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      //登陆表单的数据对象
      Loginfrom:{
        username:'',
        password:''
      },
      //表单验证对象
      LoginfromRules:{
        username:[ { required: true, message: '请输入登录名', trigger: 'blur' },
                  { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password:[{ required: true, message: '请输入密码', trigger: 'blur' },
                  { min: 6, max: 15, message: '长度在 6 到 16 个字符', trigger: 'blur' }
          ]
      }
    }
  },
  methods:{
    //重置表单resetFields()
    resetLoginFrom(){
      this.$refs.loginFormRef.resetFields();
    },
    login(){
      this.$refs.loginFormRef.validate(async valid=>{
        // console.log(valid) 为布尔值
        if(!valid) return;
        //解构赋值data并重命名为res data为服务器返回的数据
        const {data:res}= await this.$http.post('login',this.Loginfrom)
        console.log(res)
        if(res.meta.status!==200){
            return this.$message.error('登录失败');
        }else{
          this.$message.success('登录成功')
        }
        //1.将登录成功之后的token 保存到sessionStorage
        //1.1项目中除了登录的其他API接口，必须登录之后才可访问
        //1.2 token只在当前网站打开期间生效
        window.sessionStorage.setItem('token',res.data.token)
        //2.登录成功后通过编程式跳转到后台主页面
        this.$router.push('/home')
      })
    }
  }
};
</script>

<style lang='less'  scoped>
.login_container {
  background-color: skyblue;
  height: 100%;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: whitesmoke;
    border-radius: 5px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .avater_box {
      width: 80px;
      height: 80px;
      border: 1px solid #eee;
      border-radius: 50%;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      top: 5%;
      transform: translate(-50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login_from{
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .user{
        text-align: left;
      }
      .btns{
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
